<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\PayLog;
use App\Constants\PayTypeConstant;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class PayLogController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new PayLog(), function (Grid $grid) {
            $grid->model()->orderByDesc('id');

            $map = getAuthMiniProgramMap(Admin::user()->id);
            $grid->model()->whereIn('mini_type', array_keys($map));

            $grid->column('id');
            $grid->column('mini_type')->display(function ($miniType) use ($map) {
                return $miniType ? $map[$miniType] : '';
            });
            $grid->column('type', '类型')->display(function ($v){
                return PayTypeConstant::MAP[$v] ?? $v;
            });
            $grid->column('pay_sn');
            $grid->column('pay_amount')->display(function ($amount){
                return bcmul($amount, 0.01, 2);
            });
            $grid->column('pay_type');
            $grid->column('trade_no');
            $grid->column('is_pay')->using([0 => '否', 1 => '是'])->dot(['warning', 'success']);
            $grid->column('pay_time')->display(function ($time){
                return $time > 0 ? date('Y-m-d H:i:s', $time) : '';
            });
            $grid->column('notify_time')->display(function ($time){
                return $time > 0 ? date('Y-m-d H:i:s', $time) : '';
            });
            $grid->column('created_at');

            $grid->disableCreateButton();
            $grid->disableActions();

            $grid->filter(function (Grid\Filter $filter) use($map) {
                $filter->panel();
                $filter->expand();
                $filter->equal('id')->width(2);
                $filter->equal('mini_type')->select($map)->width(2);
                $filter->like('pay_sn')->width(2);
                $filter->like('trade_no')->width(2);
                $filter->equal('is_pay')->select([0 => '否', 1 => '是'])->width(2);
                $filter->between('created_at')->datetime()->width(4);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new PayLog(), function (Show $show) {
            $show->field('id');
            $show->field('mini_type');
            $show->field('type');
            $show->field('pay_sn');
            $show->field('pay_amount');
            $show->field('pay_type');
            $show->field('trade_no');
            $show->field('is_pay');
            $show->field('pay_time');
            $show->field('notify_time');
            $show->field('extend');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new PayLog(), function (Form $form) {
            $form->display('id');
            $form->text('mini_type');
            $form->text('type');
            $form->text('pay_sn');
            $form->text('pay_amount');
            $form->text('pay_type');
            $form->text('trade_no');
            $form->text('is_pay');
            $form->text('pay_time');
            $form->text('notify_time');
            $form->text('extend');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
